home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’89 / JPrint XCMD / WhichLine.p < prev   
Encoding:
Text File  |  1989-01-23  |  2.0 KB  |  96 lines  |  [TEXT/MPS ]

  1. {$R-}
  2. {
  3.     WhichLine -- an XFNC to find the line of a given character.  Used when there is
  4.                     a need to know on which line a given character falls.
  5.  
  6.     Programmer -- D. Jay Newman
  7.     Date -- 1/23/89
  8.  
  9.     This XFNC expects two parameters:
  10.         1    A piece of text to find.
  11.  
  12.         2    A container which contains text in which the first parameter lies.
  13.  
  14.     The return value is the line number in which the first string is found.  If the
  15.         first string is not found, then the return value is 0.
  16.  
  17.     It should be used like:
  18.         put WhichLine ("some text", theText) into it
  19. }
  20.  
  21. {$S WhichLine }     { Segment name must be the same as the command name. }
  22.  
  23. UNIT AnyUnit;
  24.  
  25. INTERFACE
  26.  
  27. USES MemTypes, QuickDraw, OSIntf, HyperXCmd;
  28.  
  29. PROCEDURE ENTRYPOINT(paramPtr: XCmdPtr);
  30.     
  31.  
  32. IMPLEMENTATION
  33.  
  34.  
  35. CONST
  36.     kComma            =    Ord (',');
  37.     kZero            =    Ord ('0');
  38.     kReturn            =    13;
  39.  
  40.     kNameSize        =    40;
  41.     kIDSize            =    6;
  42.  
  43. TYPE
  44.     Str31 = String[31];
  45.  
  46.  
  47. PROCEDURE WhichLine (paramPtr: XCmdPtr);  FORWARD;
  48.  
  49. PROCEDURE ENTRYPOINT(paramPtr: XCmdPtr);
  50. BEGIN
  51.     WhichLine (paramPtr);
  52. END;
  53.  
  54.  
  55.  
  56. {This is the main program}
  57. PROCEDURE WhichLine (paramPtr: XCmdPtr);
  58. VAR
  59.     p:            Ptr;                                {String to search for}
  60.     theText:    Ptr;                                {String in which to search}
  61.     s:            Str255;                                {Pattern string}
  62.     numLines:    INTEGER;                            {Number of lines}
  63.  
  64. {$I XCmdGlue.inc }
  65.  
  66. BEGIN
  67.     WITH paramPtr^ DO
  68.         BEGIN
  69.             IF paramCount < 2 THEN EXIT (WhichLine);
  70.  
  71.             HLock (params [1]);                            {lock so we can take pointer}
  72.             ZeroToPas (params[1]^, s);                    {get pascal string}
  73.             HUnlock (params [1]);                        {unlock like a good boy}
  74.  
  75.             HLock (params [2]);                            {Lock the search text and take ptr}
  76.             theText := params [2]^;
  77.  
  78.             p := StringMatch (s, theText);
  79.  
  80.             numLines := 0;
  81.             IF p <> NIL THEN
  82.                 BEGIN
  83.                     WHILE Ord4 (p) >= Ord4 (theText) DO
  84.                         BEGIN
  85.                             numLines := numLines + 1;
  86.                             ScanToReturn (theText);
  87.                             theText := Ptr (Ord4 (theText) + 1);
  88.                         END;
  89.                 END; 
  90.  
  91.             HUnlock (params[2]);                            {Unlock like a good boy}
  92.             returnValue := PasToZero (NumToStr (numLines));
  93.         END;
  94. END;        {WhichLine}
  95.  
  96. END.